package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.Contents
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.CartBean
import hlhj.fhp.supreme.javabean.LocationBean
import hlhj.fhp.supreme.javabean.WxPayBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_confirm_order_aty.*
import kotlinx.android.synthetic.main.appoint_pop.*
import com.mylhyl.superdialog.SuperDialog



class ConfirmOrderAty : BaseAty() {
    private val datas=ArrayList<CartBean.DataBean>()
    private lateinit var aid:String
    private var api: IWXAPI? = null
    private var totalMoney=""
    private lateinit var adp:BaseQuickAdapter<CartBean.DataBean,BaseViewHolder>
    override fun getContentId(): Int {
        return R.layout.activity_confirm_order_aty
    }

    override fun initView() {
        api = WXAPIFactory.createWXAPI(this, Contents.WXAPPKEY,true)
        api!!.registerApp(Contents.WXAPPKEY)
        val list = intent.getSerializableExtra("datas") as ArrayList<CartBean.DataBean>
        totalMoney=intent.getStringExtra("totalMoney")
        tvTotalPrice.text=intent.getStringExtra("totalMoney")
        tvNum.text="共${list.size.toString()}件商品"
        adp=object :BaseQuickAdapter<CartBean.DataBean,BaseViewHolder>(R.layout.confirm_item,datas){
            override fun convert(helper: BaseViewHolder?, item: CartBean.DataBean?) {
                Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemImg))
                helper?.setText(R.id.itemTittle,item?.title)
                if (intent.getStringExtra("type").equals("1")){
                    helper?.setText(R.id.itemPrice,item?.score+"积分")
                    helper?.setText(R.id.tvP,(item?.number!!*item?.score?.toDouble()!!).toString())
                }else{
                    helper?.setText(R.id.itemPrice,"￥"+item?.money)
                    helper?.setText(R.id.tvP,(item?.number!!*item?.money?.toDouble()!!).toString())
                }
            }
        }
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        recyclerview.adapter=adp


        datas.clear()
        datas.addAll(list)
        adp.notifyDataSetChanged()


        getLocation()

    }

    override fun onResume() {
        super.onResume()
        getLocation()
    }
    private fun getLocation() {
        OkGo.post<LocationBean>(Urls.BaseUrl+Urls.ADDRES_LIST)
                .execute(object :JsonCallBack<LocationBean>(this){
                    override fun onSuccess(response: Response<LocationBean>?) {
                        val body = response?.body()
                        if (body?.code == 1){
                            val list = body.data
                            var isHas=false
                            for (i in 0..list.size-1){
                                if (list[i].is_def==1){
                                    isHas=true
                                    adressPhone.text=list[i].mobile
                                    addressName.text=list[i].name
                                    addressInfo.text=list[i].province+list[i].city+list[i].area+list[i].address
                                    aid=list[i].id.toString()
                                    return
                                }
                            }
                            if (!isHas){
                                MyUtils.toast("请先选择默认收货地址")
                                startActivity(Intent(this@ConfirmOrderAty,LocationAty::class.java))
                            }
                        }
                    }
                })
    }

    override fun initListener() {
        loAddress.setOnClickListener {
            startActivity(Intent(this@ConfirmOrderAty,LocationAty::class.java))
        }
        btExit.setOnClickListener {
            finish()
        }

        btCommit.setOnClickListener {
            if (intent.getStringExtra("et").equals("1")){//1快速下单
                val bean = datas[0]
                if (intent.getStringExtra("type").equals("0")){
                    val i = Intent(this@ConfirmOrderAty, PayAty::class.java)
                    i.putExtra("et",4)
                    i.putExtra("id",bean.id.toString())
                    i.putExtra("type",intent.getStringExtra("type"))
                    i.putExtra("aid",aid)
                    i.putExtra("money", totalMoney)
                    startActivityForResult(i,1)
                }else{
                    SuperDialog.Builder(this).setRadius(10)
                            .setTitle("确定购买？").setMessage("确定使用${totalMoney}购买")
                            .setPositiveButton("确定"){
                                val i = Intent(this@ConfirmOrderAty, PayAty::class.java)
                                i.putExtra("et",4)
                                i.putExtra("id",bean.id.toString())
                                i.putExtra("type",intent.getStringExtra("type"))
                                i.putExtra("aid",aid)
                                i.putExtra("money", totalMoney)
                                startActivityForResult(i,1)
                                finish()
                            }
                            .setNegativeButton("取消"){
                                v: View? ->

                            }
                            .build()

                }
            }else{
                if (intent.getStringExtra("type").equals("0")){//购物车-人民币商品
                    var str=""
                    var isFirst=true
                    for (i in 0..datas.size-1){
                        if (isFirst){
                            isFirst=false
                            str=str+datas[i].id
                        }else{
                            str=str+","+datas[i].id
                        }
                    }
                    val i = Intent(this@ConfirmOrderAty, PayAty::class.java)
                    i.putExtra("et",3)
                    i.putExtra("ids",str)
                    i.putExtra("type",intent.getStringExtra("type"))
                    i.putExtra("aid",aid)
                    i.putExtra("money", totalMoney)
                    startActivityForResult(i,1)

                }else{
                    SuperDialog.Builder(this)
                            .setTitle("确认购买？").setMessage("确认使用${totalMoney}积分购买？")
                            .setRadius(10)
                            .setPositiveButton("确定"){
                                v: View? ->
                                var str=""
                                var isFirst=true
                                for (i in 0..datas.size-1){
                                    if (isFirst){
                                        isFirst=false
                                        str=str+datas[i].id
                                    }else{
                                        str=str+","+datas[i].id
                                    }
                                }
                                val i = Intent(this@ConfirmOrderAty, PayAty::class.java)
                                i.putExtra("et",3)
                                i.putExtra("ids",str)
                                i.putExtra("type",intent.getStringExtra("type"))
                                i.putExtra("aid",aid)
                                i.putExtra("money", totalMoney)
                                startActivityForResult(i,1)
                                finish()
                            }
                            .setNegativeButton("取消"){
                                v: View? ->

                            }
                            .build()

                    /*OkGo.post<WxPayBean>(Urls.BaseUrl+Urls.CART_ORDER)
                            .params("ids",str)
                            .params("type",intent.getStringExtra("type"))
                            .params("aid",aid)
                            .params("pay_type",1)
                            .execute(object :JsonCallBack<WxPayBean>(this){
                                override fun onSuccess(response: Response<WxPayBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        val data = bean?.data
                                        MyUtils.toast("提交订单成功")

                                    *//*    setResult(-1)
                                        finish()*//*

                                    }else{
                                        MyUtils.toast(bean?.msg)
                                    }
                                }
                            })*/
                }
            }


        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode==-1)finish()
    }
}
